Why does my variable determining an item in a list become out of range